home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / c / hce.lha / HCE / LibSource / clib / Misc / src / CliParse.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-02  |  2.4 KB  |  122 lines

  1. #include <libraries/dosextens.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <errno.h>
  5.  
  6. extern int      _argc;
  7. extern char     **_argv;
  8.  
  9. typedef struct Process PROCESS;
  10. typedef struct CommandLineInterface CLI;
  11.  
  12. static char errmsg[] = "Too many arguments\n";
  13. static char *_malloc(size)
  14. int size;
  15. /*
  16.  * Internal error checking interface for the malloc() function
  17.  */
  18.     {
  19.     register char *p;
  20.     char *malloc();
  21.     long Output();
  22.  
  23.     if(p = malloc(size))
  24.         return(p);
  25.     Write( Output(), errmsg, 19L);
  26.     _exit(ENOMEM);
  27.     }
  28.     
  29. /*
  30.  * When invoked from the CLI, sets up argc and argv[].
  31.  */
  32. void _cli_parse(tp, cmdlen, cmdline)
  33. PROCESS *tp;
  34. int cmdlen;
  35. char *cmdline;
  36. {
  37.     register unsigned char *p, *q;
  38.     register int i, n;
  39.     unsigned char *argbuf, delim;
  40.     CLI    *cli;
  41.  
  42.     /*
  43.      * Get the name I was called by.
  44.      */
  45.  
  46.     cli = (CLI *)(tp->pr_CLI << 2);
  47.     p = (unsigned char *) ( cli->cli_CommandName << 2);
  48.     i = p[0];
  49.     argbuf  = (unsigned char *)_malloc( (int)(i + cmdlen + 2) );
  50.  
  51.     /*
  52.      * copy the command line 'cause we're going to munge it.
  53.      */
  54.  
  55.     strncpy( argbuf, &p[1], i);
  56.     argbuf[i] = ' ';
  57.     argbuf[i+1] = '\0';
  58.     strncat( argbuf, cmdline, (int)cmdlen);
  59.  
  60.     /*
  61.      * Trim any trailing non-white space (like a newline).
  62.      */
  63.  
  64.     p = &argbuf[i+cmdlen+1];
  65.     while ( p > argbuf && *p < ' ')
  66.         *p-- = '\0';
  67.  
  68.     /*
  69.      * Break the copy of the command line into tokens based on
  70.      * the break characters " \t".  Break characters within double
  71.      * quotation marks will be ignored.
  72.      *
  73.      * This is tricky since we are doing an in-place compression to
  74.      * eliminate leading and break characters.
  75.      * p points to next character to be tested in argbuf[].
  76.      * q points to where the next character should be put in argbuf[].
  77.      */
  78.  
  79.     /*
  80.      * parse command line image based on whitespace or delimiter.
  81.      */
  82.  
  83.     n = 0;
  84.     for ( p=q=argbuf; *p; ++n,p++ ) {
  85.         while ( *p == ' ' || *p == '\t' )
  86.             p++;
  87.         if ( p == (unsigned char *)NULL)
  88.             break;
  89.  
  90.         if ( *p != '\"' ) {
  91.             do {
  92.                 *q++ = *p++;
  93.             } while ( *p && *p != ' ' && *p != '\t' );
  94.         } else {
  95.             delim = *p++;
  96.             while ( *p && *p != delim )
  97.                 *q++ = *p++;
  98.         }
  99.  
  100.         /*
  101.          * Mark the end of this string in argbuf[].
  102.          */
  103.     
  104.         *q++ = '\0';
  105.     
  106.     }
  107.  
  108.     *q = '\0';
  109.     /*
  110.      * Allocate _argv array and store the indexes into the argbuf
  111.      * string.
  112.      */
  113.     _argv = (char **)_malloc( (n+1) * sizeof( char *));
  114.     p = argbuf;
  115.     for ( i=0; i<n; i++ ) {    
  116.         _argv[i] = (char *)p;
  117.         p += strlen(p) + 1;
  118.     }
  119.     _argv[i] = (char *)NULL;
  120.     _argc = n;
  121. }
  122.